package controllers;

import play.*;
import play.mvc.*;
import sun.text.normalizer.CharTrie.FriendAgent;

import java.util.*;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import common.FBSecure;
import common.FBUtil;

import models.*;

@With(FBSecure.class)
public class Application extends Controller {
	

	public static void index() {

		User currentUser = FBController.getUser();
		if (currentUser == null) {
			notFound("INVALID_USER"); // we should not be here!
		}

		String oauthToken = FBController.getOAuthToken();
		if (currentUser == null) {
			notFound("INVALID_AUTH_TOKEN"); // we should not be here!
		}

		JsonElement jeUser = FBUtil.getInstance().executeGraphRequest("/me",
				oauthToken);

		if ((jeUser != null) && (jeUser.isJsonObject())
				&& (jeUser.getAsJsonObject().has("name"))) {
			currentUser.name = jeUser.getAsJsonObject().get("name")
					.getAsString();
			currentUser.save();
		}
		
		
		
		// getFriendLikes method
		List<String> friendNames = new ArrayList<String>();
		List<String> friendProfilePictures = new ArrayList<String>();
		List<String> friendLikes = new ArrayList<String>();
		
		
		JsonElement jeUserFriends = FBUtil.getInstance().executeGraphRequest(
				"/me/friends", oauthToken);
		

		if ((jeUserFriends != null) && (jeUserFriends.isJsonObject())
				&& (jeUserFriends.getAsJsonObject().has("data"))) {
			JsonArray ja = jeUserFriends.getAsJsonObject().get("data").getAsJsonArray();
					
			JsonObject jo = null;

			for (int i = 0; friendNames.size() < 5/* ja.size() */; i++) {
				jo = ja.get(i).getAsJsonObject();
				String friendFacebookId = jo.get("id").getAsString();
				String friendName = jo.get("name").getAsString();
				
				
//				UserFriend userFriend = new UserFriend(friendName, friendFacebookId, currentUser);
//				userFriend.save();
				

				String query = "SELECT name FROM page WHERE page_id IN (SELECT page_id FROM page_fan WHERE uid="
						+ friendFacebookId + " AND type='MOVIE') LIMIT 1";
				JsonElement jeFriendsLikes = FBUtil.getInstance().executeFQLQuery(oauthToken, query);
				JsonElement jeeee = jeFriendsLikes.getAsJsonObject();
				if ((jeeee != null) && (jeeee.getAsJsonObject().has("data"))) {
					
					JsonElement jeLike = jeeee.getAsJsonObject().get("data");
					JsonArray jaLikes = jeLike.getAsJsonArray();
					
					String likeName = null;
					for (int j = 0; j < jaLikes.size(); j++) {
						JsonObject joLikes = jaLikes.get(j).getAsJsonObject();
						likeName = joLikes.get("name").getAsString();
						
//						MovieLike movieLike = new MovieLike(likeName);
//						movieLike.save();
					}
					
					
					String pictureUrl = null;
					String pictureQuery = "SELECT uid, pic_square FROM user where uid=" + friendFacebookId;
					JsonElement jeProfilePicture = FBUtil.getInstance().executeFQLQuery(oauthToken, pictureQuery);
					
					if ((jeProfilePicture != null) && (jeProfilePicture.isJsonObject())
							&& (jeProfilePicture.getAsJsonObject().has("data"))){
						JsonArray jaProfilePicture = jeProfilePicture.getAsJsonObject().get("data").getAsJsonArray();
						JsonObject joProfilePicture = jaProfilePicture.get(0).getAsJsonObject();
						
						pictureUrl = joProfilePicture.get("pic_square").getAsString();
					}
					
					if((likeName != null) && (friendName != null) && (pictureUrl != null)) {
						friendLikes.add(likeName);
						friendNames.add(friendName);
						friendProfilePictures.add(pictureUrl);
					}
				}
				
			}	
		}
		// end getFriendsLikes method
		
		
		
		render(friendNames, friendLikes, friendProfilePictures);
	}
	
	public static void moviepage2() {
		render();
	}
	
	
	// deneme
	public static void moviepage3() {
		List<Movie> movies = Movie.findAll();
		render(movies);
	}
	// deneme
	
	public static void createMovie() {
		render();
	}
	
	public static void saveCreatedMovie(Movie movie) {
		movie.save();
		show(movie.getId());
	}
	
	public static void getMovieImage(Long id) {
		Movie movie = Movie.findById(id);
		renderBinary(movie.image.get());
	}
	
	public static void show(Long id) {
		Movie movie = Movie.findById(id);
		render(movie);
	}
	
	public static void getFriendLikes(String oauthToken, User currentUser) {
		
	} // end of getFriendLikes method
	
}